package com.zhiche.lisa.mdm.controller.inner;

import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.mdm.dao.model.CardIssuerType;
import com.zhiche.lisa.mdm.service.ICardIssuerTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 发卡方类型 接口
 * @since 2018-06-01
 * @author lbl
 */
@RestController
@RequestMapping("/inner/cardIssuerType")
@Api(value = "/inner/cardIssuerType", description = "发卡方类型接口",tags={"发卡方类型接口"})
public class CardIssuerTypeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CardIssuerTypeController.class);

    @Autowired
    private ICardIssuerTypeService cardIssuerTypeService;

    @GetMapping("/selectByVal")
    @ApiOperation(value = "根据名称获取发卡方类型", notes = "根据名称获取发卡方类型", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<List<CardIssuerType>> selectByVal(String val) {
        LOGGER.info("CardIssuerTypeController.selectByVal（根据 val 获取发卡方类型） params : {} ", val);

        RestfulResponse<List<CardIssuerType>> result = new RestfulResponse<>(0,"success",null);
        try {
            result.setData(cardIssuerTypeService.selectListByValue(val));
        } catch (BaseException e) {
            LOGGER.error("CardIssuerTypeController.selectByVal ERROR... ", e);
            result = new RestfulResponse<>(e.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("CardIssuerTypeController.selectByVal ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @GetMapping("/selectByCode")
    @ApiOperation(value = "根据编码获取发卡方类型", notes = "根据编码获取发卡方类型", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<String> selectByCode(String code) {
        LOGGER.info("CardIssuerTypeController.selectByCode（根据编码获取发卡方类型） params : {} ", code);

        RestfulResponse<String> result = new RestfulResponse<>(0,"success",null);
        try {
            List<CardIssuerType> list = cardIssuerTypeService.selectListByCode(code);
            if(!CollectionUtils.isEmpty(list)){
                result.setData(list.get(0).getValue());
            }
        } catch (BaseException e) {
            LOGGER.error("CardIssuerTypeController.selectByCode ERROR... ", e);
            result = new RestfulResponse<>(e.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("CardIssuerTypeController.selectByCode ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @GetMapping("/selectById")
    @ApiOperation(value = "根据ID获取发卡方类型", notes = "根据ID获取发卡方类型", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<String> selectById(Integer id) {
        LOGGER.info("CardIssuerTypeController.selectById（根据ID获取发卡方类型） params : {} ", id);

        RestfulResponse<String> result = new RestfulResponse<>(0,"success",null);
        try {
            CardIssuerType cardIssuerType = cardIssuerTypeService.selectById(id);
            if(Objects.nonNull(cardIssuerType)){
                result.setData(cardIssuerType.getValue());
            }
        } catch (BaseException e) {
            LOGGER.error("CardIssuerTypeController.selectById ERROR... ", e);
            result = new RestfulResponse<>(e.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("CardIssuerTypeController.selectById ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

}
